package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.PublicModle;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.tools.ImageUtils;

import java.util.List;

/**
 * Created by Yee3 on 2017/1/9.
 */
public class PublicFragmentViewPagerAdapter extends BaseRecyclerAdapter<PublicFragmentViewPagerAdapter.viewHodler> {
    private List<PublicModle> modleList;
    private Context context;

    public PublicFragmentViewPagerAdapter(List<PublicModle> modleList,Context context){
        this.modleList=modleList;
        this.context=context;
    }

    @Override
    public viewHodler getViewHolder(View view) {
        return new viewHodler(view);
    }

    @Override
    public viewHodler onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v= LayoutInflater.from(context).inflate(R.layout.fragment_public_viewpager_item,parent,false);
        return new viewHodler(v);
    }

    @Override
    public void onBindViewHolder(viewHodler holder, int position, boolean isItem) {
        PublicModle modle=modleList.get(position);
        holder.id = modle.getId();
        holder.tv_title.setText(modle.getTitle());
        ImageUtils.getInstance().ImageStart(context,modle.getImage(),holder.image_rl);
        int state=modle.getState();
        if(state==0){
            holder.img_topleft.setImageResource(R.mipmap.playstatepicon1);
        }else if(state==1){
            holder.img_topleft.setImageResource(R.mipmap.playstatepicon2);
        }else if(state==2){
            holder.img_topleft.setImageResource(R.mipmap.playstatepicon3);
        }
        holder.image_rl.setScaleType(ImageView.ScaleType.FIT_XY);
//        holder.img_topleft.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.tv_price.setText(modle.getPrice());
        holder.tv_date.setText(modle.getData());
        holder.webview=modle.getWebview();

    }

    @Override
    public int getAdapterItemCount() {
        return modleList.size();
    }

    class viewHodler extends RecyclerView.ViewHolder{
        private ImageView image_rl,img_topleft;
        private TextView tv_title,tv_price,tv_date;
        private String id;
        private String webview;
        public viewHodler(View itemView) {
            super(itemView);
            image_rl= (ImageView) itemView.findViewById(R.id.image_rl);
            img_topleft= (ImageView) itemView.findViewById(R.id.img_topleft);
            tv_title= (TextView) itemView.findViewById(R.id.tv_name);
            tv_price= (TextView) itemView.findViewById(R.id.tv_price);
            tv_date= (TextView) itemView.findViewById(R.id.tv_date);
            /**
             * 每一个item的点击事件
             */
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.OnItemClick(view,getLayoutPosition()-1);
                }
            });

        }
    }

    /**
     * 点击每一行的接口
     */
    private OnItemClicklistener onItemClickListener;
    public interface  OnItemClicklistener{
        void OnItemClick(View view,int position);
    }

    /**
     * 单行点击事件
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClicklistener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    /**
     * 清空适配器数据
     */
    public void clear(){
        clear(modleList);
    }

    /**
     * 添加数据
     */
    public void insert(PublicModle modle,int position){
        insert(modleList,modle,position);
    }
}
